package com.stooscloud.provider;

import com.stooscloud.base.exception.BusinessException;
import com.stooscloud.core.support.SpringContextHolder;
import com.stooscloud.provider.builder.Builder;
import com.stooscloud.provider.config.TransInitRunner;
import com.stooscloud.wrapper.Wrapper;
import com.stooscloud.wrapper.WrapMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.net.URLEncoder;

@Component
@Slf4j
public class RequestHanlder {
    @Autowired
    TransInitRunner transInitRunner;
    String jsonValue;
    public Wrapper handler(HttpServletRequest request)throws BusinessException{
        String transCode="";
        if (isJson(request)){
            jsonValue = jsonReq(request);
            transCode = getTransCode(jsonValue);
        }else {
            transCode = getParame(request,"tranCode");
        }
        log.info("请求交易的交易码【transcode】:{}",transCode);
        Builder builder = getBuilder(transCode);
        if(builder != null){
           return compose(builder,request);
        }else {
            return WrapMapper.wrap("S1005","请求报⽂文解析失败");
        }

    }
    private String getTransCode(String json){
        String str = StringUtils.substringBetween(json,"tranCode",",");
        return StringUtils.substringBetween(str,":\"","\"");
    }
    private String getParame(HttpServletRequest request,String key){
       return request.getParameter(key);
    }

    private boolean isJson(HttpServletRequest request){
        String contentType = request.getHeader("Content-Type");
        if(StringUtils.isNotBlank(contentType) && StringUtils.contains(contentType,"json")){
            return true;
        }else {
            return false;
        }
    }

    private Builder getBuilder(String transCode){
        if (StringUtils.isNotBlank(transCode)){
            String beanName = transInitRunner.getBuilder(transCode);
            log.info("交易映射的处理类【beanName】:{}",beanName);
            return SpringContextHolder.getBean(beanName);
        }else {
            return null;
        }
    }
    private Wrapper compose(Builder builder,HttpServletRequest request) throws BusinessException{
        if (isJson(request)){
            builder.createDoman(jsonValue);
        }else {
            builder.createDoman(request);
        }
        builder.handler();
        builder.idBus();
        builder.call();
        return builder.installWrapper();
    }

    private static String jsonReq(HttpServletRequest request) {
        BufferedReader br;
        StringBuilder sb = null;
        String reqBody = null;
        try {
            br = new BufferedReader(new InputStreamReader(request.getInputStream()));
            String line = null;
            sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            reqBody = URLEncoder.encode(sb.toString(),"UTF-8");
            reqBody = URLDecoder.decode(reqBody, "UTF-8");
            log.info("接收的json报文信息【json】:{}",reqBody);
        } catch (IOException e) {
            log.error("获取json报文失败:{}",e.getMessage());
            e.printStackTrace();
        }
        return reqBody;
    }
}
